//
//  HXDUserAccount.swift
//  weibo
//
//  Created by huxuedong on 15/12/6.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

class HXDUserAccount: NSObject, NSCoding {
    
    //用户id
    var uid: String?
    //过期时间(秒)
    var expires_in: Int = 0 {
        didSet {
            //过期日期 = 当前日期 + 过期时间
            expiresDate = NSDate(timeIntervalSinceNow: Double(expires_in))
        }
    }
    //接口获取授权后的access_token
    var access_token: String?
    //过期日期
    var expiresDate: NSDate?
    //用户头像
    var avatar_large: String?
    //用户昵称
    var screen_name: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    //重写description函数，输出详细信息（默认只输出名称和地址）
    override var description: String {
        let keys = ["uid", "expires_in", "access_token", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
}
